package com.hznu.coursemanagerbackend.modules.sys.controller;


import com.hznu.coursemanagerbackend.modules.sys.entity.TeacherInfo;
import com.hznu.coursemanagerbackend.modules.sys.entity.User;
import com.hznu.coursemanagerbackend.modules.sys.service.TeacherInfoService;
import com.hznu.coursemanagerbackend.modules.sys.service.UserService;
import com.hznu.coursemanagerbackend.myBeans.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DragonDoor
 * @since 2018-11-12
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping("/user")
@Api(description = "用户管理接口",value = "user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TeacherInfoService teacherInfoService;

    @ApiOperation("添加用户")
    @PostMapping("/add")
    public R addUser(@RequestBody User user) throws Exception{
        try{
            userService.add(user);
            if(user.getUsertype()==1){
                TeacherInfo teacherInfo = new TeacherInfo();
                teacherInfo.setUserid(user.getId());
                teacherInfoService.insert(teacherInfo);
            }
            return R.ok("添加成功");
        }catch(Exception e){
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
}

